﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using estudent.Models;

namespace estudent.Controllers
{
    public class ObcinaSifrantController : Controller
    {
        private estudentEnt db = new estudentEnt();
        int? stara = null;
        //
        // GET: /ObcinaSifrant/

        public ActionResult Index()
        {
            return View(db.SObcines.OrderBy(p => p.imeObcina).ToList());
        }

        //
        // GET: /ObcinaSifrant/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /ObcinaSifrant/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /ObcinaSifrant/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "veljavno")] SObcine ustvarjena)
        {
            if (ustvarjena.sObcina.ToString().Length > 3)
                ModelState.AddModelError("sObcina", "Predolgo");
            try
            {
                if (!ModelState.IsValid)
                    return View();

                db.AddToSObcines(ustvarjena);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("sObcina", "Ni unikat");
                return View();
            }
        }

        //
        // GET: /ObcinaSifrant/Edit/5

        public ActionResult Edit(int id)
        {
            var uredi = (from p in db.SObcines
                         where p.sObcina == id
                         select p).First();
            stara = id;

            return View(uredi);
        }

        //
        // POST: /ObcinaSifrant/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit([Bind(Exclude = "veljavno")] SObcine urejen)
        {
            if (urejen.sObcina.ToString().Length > 3)
                ModelState.AddModelError("sObcina", "Predolgo");
            if (urejen.sObcina != stara && db.SObcines.Any(p => p.sObcina == urejen.sObcina))
                ModelState.AddModelError("sObcina", "Obstaja");

            try
            {
                var original = (from p in db.SObcines
                                where p.sObcina == urejen.sObcina
                                select p).First();

                if (!ModelState.IsValid)
                    return View(original);

                db.ApplyCurrentValues(original.EntityKey.EntitySetName, urejen);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("sObcina", "Ni unikat");
                return View();
            }
        }

        //
        // GET: /ObcinaSifrant/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                var orig = (from p in db.SObcines
                            where p.sObcina == id
                            select p).First();
                var spremen = (from p in db.SObcines
                               where p.sObcina == id
                               select p).First();

                if (orig.veljavno != null)
                {
                    spremen.veljavno = !orig.veljavno;
                }
                else
                {
                    spremen.veljavno = false;
                }

                db.ApplyCurrentValues(orig.EntityKey.EntitySetName, spremen);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /ObcinaSifrant/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
